﻿package {
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.filters.*;

	public class Gift extends Sprite {
		private var giftArray:Array;
		private var randomArray:Array;
		private var giftNum:uint;
		private var glowColor:Array;
		private var giftXs:Array;
		private var giftY:int;
		private var giftTimer:Timer;
		private var speed:Array;
		private var glowSpeed:Number;
		private var filter1:GlowFilter;
		private var filter2:BlurFilter;
		private var currentOverMC:MovieClip;
		private var currentIndex:int;
		private var bonus:Bonus;

		public function get giftIndex():int {
			if (currentIndex!=-1) {
				return randomArray[currentIndex];
			}
			return -1;
		}

		public function Gift(GIFTFUN:Function):void {
			init();
			var i:int;
			for (i=0; i<giftNum; i++) {
				giftArray[i]=new Gifts();
				giftArray[i].gotoAndStop(i+1);
				giftArray[i].x=giftXs[randomArray[i]];
				giftArray[i].y=-70;
				addChild(giftArray[i]);
			}
			giftTimer=new Timer(10);
			giftTimer.addEventListener(TimerEvent.TIMER,giftTiming);
			//giftTimer.addEventListener(TimerEvent.TIMER_COMPLETE ,giftTimed);
			giftTimer.start();
			this.addEventListener(Game.GETGIFT.type,GIFTFUN);

		}
		private function init():void {
			giftNum=3;
			giftArray=new Array(giftNum);
			glowColor=new Array(0xFFFF00,0x65FF00,0x32CCFF);
			giftXs=new Array(112,275,438);
			giftY=200;
			speed=new Array(1,1,1);
			randomArray=SetRandomArray(3);
			//trace("Gift,randomArray",randomArray);
			filter1 = new GlowFilter ();
			filter1.strength=2;
			filter1.blurX=0;
			filter1.blurY=0;
			filter1.quality=BitmapFilterQuality.MEDIUM;
			glowSpeed=.5;
			filter2 = new BlurFilter ();
			filter2.blurX=0;
			filter2.blurY=0;
			filter2.quality=BitmapFilterQuality.LOW;
			currentIndex=-1;
		}
		private function giftTiming(e:TimerEvent):void {//礼物盒子下落动画
			var i:int;
			if (giftArray[randomArray[0]].y<giftY) {
				giftArray[randomArray[i]].y+=speed[randomArray[0]];
				speed[randomArray[0]]+=.2;
			}
			for (i=1; i<giftNum; i++) {
				if (giftArray[randomArray[i-1]].y>=giftY&&giftArray[randomArray[i]].y<giftY) {
					giftArray[randomArray[i]].y+=speed[randomArray[i]];
					speed[randomArray[i]]+=.2;
				}
			}
			if (giftArray[randomArray[giftNum-1]].y>=giftY) {
				dropfinished();
			}
		}
		private function dropfinished():void {
			giftTimer.removeEventListener(TimerEvent.TIMER,giftTiming);
			var i:int;
			for (i=0; i<giftNum; i++) {
				giftArray[i].buttonMode=true;
				giftArray[i].addEventListener(MouseEvent.MOUSE_OVER,mouseover);
				giftArray[i].addEventListener(MouseEvent.MOUSE_OUT,mouseout);
				giftArray[i].addEventListener(MouseEvent.CLICK,onclick);
			}
			giftTimer.addEventListener(TimerEvent.TIMER,giftTiming2);
		}
		private function giftTiming2(e:TimerEvent):void {//盒子发光滤镜
			var i:int;
			if (filter1.blurX<35) {
				filter1.blurX+=1;
				filter1.blurY+=1;
			} else {
				if (filter1.blurX==35||filter1.blurX==50) {
					glowSpeed=- glowSpeed;
				}
				filter1.blurX+=glowSpeed;
				filter1.blurY+=glowSpeed;
			}
			for (i=0; i<giftNum; i++) {
				filter1.color=glowColor[i];
				giftArray[i].filters=[filter1];
			}
		}
		private function giftTiming3(e:TimerEvent):void {//选择盒子后，盒子消退动画
			//if (filter1.blurX<35) {
			filter2.blurX+=2;
			filter2.blurY+=2;
			//} 
			currentOverMC.filters=[filter2];
			currentOverMC.alpha-=.02;
			if (currentOverMC.alpha<=0) {
				giftTimer.removeEventListener(TimerEvent.TIMER,giftTiming3);
				bonus=new Bonus();
				bonus.x=currentOverMC.x;
				bonus.y=currentOverMC.y;
				removeChild(currentOverMC);
				var frame:uint;
				if (randomArray[currentIndex]>0) {
					frame=2;
				} else {
					frame=1;
				}
				bonus.txt.gotoAndStop(frame);
				addChild(bonus);
				var bonusTimer:Timer=new Timer(1200,1);
				bonusTimer.addEventListener(TimerEvent.TIMER_COMPLETE,finished);
				bonusTimer.start();
				//bonus.addEventListener(MouseEvent.CLICK,finished);
			}
		}
		private function giftTiming4(e:TimerEvent):void {//礼物消退动画
			//if (filter1.blurX<35) {
			filter2.blurX+=5;
			filter2.blurY+=5;
			//} 
			bonus.filters=[filter2];
			bonus.alpha-=.02;
			if (bonus.alpha<=0) {
				giftTimer.removeEventListener(TimerEvent.TIMER,giftTiming4);
				removeChild(bonus);
				this.dispatchEvent(Game.GETGIFT);
			}
		}
		private function finished(e:TimerEvent):void {
			giftTimer.addEventListener(TimerEvent.TIMER,giftTiming4);
		}
		private function onclick(e:MouseEvent):void {
			currentOverMC=e.target;
			var i:int;
			for (i=0; i<giftNum; i++) {
				giftArray[i].removeEventListener(MouseEvent.MOUSE_OVER,mouseover);
				giftArray[i].removeEventListener(MouseEvent.MOUSE_OUT,mouseout);
				giftArray[i].removeEventListener(MouseEvent.CLICK,onclick);
				if (currentOverMC!=giftArray[i]) {
					removeChild(giftArray[i]);
				} else {
					currentIndex=i;
				}
			}
			giftTimer.addEventListener(TimerEvent.TIMER,giftTiming3);
		}
		private function mouseover(e:MouseEvent):void {
			currentOverMC=e.target;
			giftTimer.addEventListener(TimerEvent.TIMER,mouseoverTiming);
		}
		private function mouseout(e:MouseEvent):void {
			if (currentOverMC!=null) {
				giftTimer.removeEventListener(TimerEvent.TIMER,mouseoverTiming);
				currentOverMC.scaleX=1;
				currentOverMC.scaleY=1;
			}
		}
		private function mouseoverTiming(e:TimerEvent):void {
			if (currentOverMC.scaleX<1.1) {
				currentOverMC.scaleX+=.01;
				currentOverMC.scaleY+=.01;
			}
		}
		private function SetRandomArray(len:uint):Array {
			//trace("SetRandomArray",len);
			var randomarray=new Array(len);
			var i:uint;
			var temp:int;
			randomarray[0]=Math.floor(Math.random()*len);
			for (i=1; i<len; i++) {
				temp=-1;
				while (temp==-1) {
					temp=Math.floor(Math.random()*len);
					for (var j=0; j<i; j++) {
						if (temp==randomarray[j]) {
							temp=-1;
						}
					}
					if (temp!=-1) {
						randomarray[i]=temp;
					}
				}
			}
			//trace("SetRandomArray",randomarray);
			return randomarray;
		}
	}
}